package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.im.table.IMContactGroup;

import java.util.ArrayList;
import java.util.List;

public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.MyViewHolder> {

    Context mContext;
    List<IMContactGroup> list = new ArrayList<>();

    public GroupListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setGroupList(List<IMContactGroup> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_contact_my_friend_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        IMContactGroup item = list.get(position);
        holder.headerTv.setVisibility(View.GONE);
        Glide.with(mContext).load(item.protraitUrl).placeholder(R.mipmap.group_def_avatar)
                .transforms(new CenterCrop(), new RoundedCorners(15))
                .into(holder.avatar);
        holder.nameText.setText(item.name);
        holder.itemView.setOnClickListener(view -> iOnDataClickItem.onClick(position));
    }

    public IMContactGroup getItem(int index){
        return list.get(index);
    }

    private OnItemClickListener iOnDataClickItem;

    public void setOnDataClickItem(OnItemClickListener iOnClickItem) {
        this.iOnDataClickItem = iOnClickItem;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTv;
        TextView nameText;
        TextView headerTv;
        ImageView avatar;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstTv = itemView.findViewById(R.id.first_letter_tv);
            nameText = itemView.findViewById(R.id.mText_item_contact_name);
            headerTv = itemView.findViewById(R.id.mText_list_item_header);
            avatar = itemView.findViewById(R.id.img_avatar);
        }
    }
}
